package base.classtest.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class FieldChangeMain {

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		Class<?> cls = Class.forName("base.classtest.reflect.A");
		Field filed = null;
		try {
			filed = cls.getDeclaredField("AA");//可以获取private修饰的成员
			filed.setAccessible(true);//private 修饰的成员变量在修改是需要用setAccessible
		} catch (NoSuchFieldException e2) {
			e2.printStackTrace();
		}
		try {
			cls.getField("AA");//这里只能获取所有从父类继承过来的和它自己定义的, public 修饰的属性, private 修饰的属性不能获取
		} catch (NoSuchFieldException e1) {
			e1.printStackTrace();
		}
		Field[] fields = cls.getFields();
		A a = (A) cls.newInstance();
		filed.set(a, 23);//private 修饰的成员变量在修改是需要用setAccessible
		System.out.println(a.getA());
		for (Field field : fields) {
			if("sss".equals(field.getName())) {
				field.setAccessible(true);
				try {
					field.set(a, "fdsfsdfsdfsd");
					System.out.println(a.geta());
				} catch (IllegalArgumentException | IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
